from tortoise.models import Model
from tortoise import fields
from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime


# Tortoise-ORM 模型
class Record(Model):
    record_id = fields.IntField(pk=True)  # 记录ID（主键）

    # 多对多关系的两个外键
    student = fields.ForeignKeyField(
        "models.Student",
        related_name="records",
        on_delete=fields.CASCADE
    )
    course = fields.ForeignKeyField(
        "models.Class",
        related_name="records",
        on_delete=fields.CASCADE
    )

    # 可选：添加额外字段
    enroll_time = fields.DatetimeField(auto_now_add=True)  # 选课时间
    status = fields.CharField(max_length=20, default="active")  # 选课状态

    class Meta:
        table = "records"
        # 添加联合唯一约束，防止重复选课
        unique_together = (("student", "course"),)


# ---------- Pydantic 模型 ----------

        from_attributes = True